/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Lists;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Exceptions;
using System.Net;
using DotNetNuke.Entities.Tabs;
using System.Collections;
using DotNetNuke.Security.Permissions;

namespace DNNspot.Sitemap
{
    public partial class Settings : ModuleSettingsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int portalToUse = !String.IsNullOrEmpty(Convert.ToString(ModuleSettings["PortalToUse"])) ? Convert.ToInt32(ModuleSettings["PortalToUse"]) : PortalId;
                string portalAliasToUse = !String.IsNullOrEmpty(Convert.ToString(ModuleSettings["PortalAliasToUse"])) ? Convert.ToString(ModuleSettings["PortalAliasToUse"]) : String.Empty;
                if (UserInfo.IsSuperUser)
                {
                    pnlSuperUsers.Visible = true;
                    int value = int.MaxValue;
                    var portals = PortalController.GetPortalsByName("%", 0, int.MaxValue, ref value);

                    foreach (PortalInfo portal in portals)
                    {
                        ListItem item = new ListItem(portal.PortalName + " - PortalId: " + portal.PortalID, portal.PortalID.ToString());
                        ddlPortalListing.Items.Add(item);
                    }

                    ddlPortalListing.SelectedValue = portalToUse.ToString();
                }

                txtTabIdExclude.Text = Convert.ToString(ModuleSettings["TabIdExclude"]);
                chkIncludeHiddenPages.Checked = Convert.ToBoolean(ModuleSettings["IncludeHidden"]);
                chkShowViewEditPermissions.Checked = Convert.ToBoolean(ModuleSettings["ShowViewEditPermissions"]);

                //SET LEVELS FOR SITEMAP 
                ddlLevels.Items.Add("0");
                ddlLevels.Items.Add("1");
                ddlLevels.Items.Add("2");
                ddlLevels.Items.Add("3");
                ddlLevels.Items.Add("4");
                ddlLevels.Items.Add("5");

                int drplevel = Convert.ToInt32(ModuleSettings["_SitemapLevel"]);
                //ddlLevels.Items.Add(drplevel.ToString());
                ddlLevels.SelectedIndex = drplevel;

                //SET PAGE OPTIONS FOR STARTING POINTS


                int pagestart = Convert.ToInt32(ModuleSettings["SitemapStart"]);

                LoadPortalAliasesByPortalId(portalToUse);
                LoadTabsByPortalId(portalToUse);

                ddlPortalAlias.SelectedValue = portalAliasToUse;
                ddlPages.SelectedValue = pagestart.ToString();
            }
        }

        private void LoadTabsByPortalId(int portalId)
        {
            ddlPages.Items.Clear();
            TabController tabcontroller = new TabController();
            ArrayList alltabs = tabcontroller.GetTabs(portalId);
            ddlPages.Items.Add(new ListItem("ALL/ROOT", "-1"));

            foreach (TabInfo tab in alltabs)
            {
                if (tab.IsDeleted == false)
                {
                    string indent = IndentAmount(tab.Level);
                    ListItem item = new ListItem(indent + tab.TabName + " - tabid: " + tab.TabID, tab.TabID.ToString());
                    ddlPages.Items.Add(item);
                }
            }
        }

        string IndentAmount(int level)
        {
            string indent = "";
            for (int i = 0; i < level; i++)
            {
                indent += "-";
            }
            return indent;
        }

        public override void UpdateSettings()
        {
            ModuleController settings = new ModuleController();
            settings.UpdateModuleSetting(ModuleId, "TabIdExclude", txtTabIdExclude.Text);
            settings.UpdateModuleSetting(ModuleId, "IncludeHidden", chkIncludeHiddenPages.Checked.ToString());
            settings.UpdateModuleSetting(ModuleId, "ShowViewEditPermissions", chkShowViewEditPermissions.Checked.ToString());
            settings.UpdateModuleSetting(ModuleId, "_SitemapLevel", ddlLevels.SelectedIndex.ToString());
            settings.UpdateModuleSetting(ModuleId, "SitemapStart", ddlPages.SelectedValue);
            settings.UpdateModuleSetting(ModuleId, "PortalToUse", ddlPortalListing.SelectedValue);
            settings.UpdateModuleSetting(ModuleId, "PortalAliasToUse", ddlPortalAlias.SelectedValue);
        }

        protected void ddlPortalListing_SelectedIndexChanged(object sender, EventArgs e)
        {
            var portalId = Convert.ToInt32(ddlPortalListing.SelectedValue);
            LoadPortalAliasesByPortalId(portalId);
            LoadTabsByPortalId(portalId);
        }

        private void LoadPortalAliasesByPortalId(int portalId)
        {
            ddlPortalAlias.Items.Clear();

            PortalAliasController portalAliasController = new PortalAliasController();
            PortalAliasCollection portalAliases = portalAliasController.GetPortalAliasByPortalID(portalId);

            foreach (DictionaryEntry portalAlias in portalAliases)
            {

                ListItem item = new ListItem(portalAlias.Key.ToString());
                ddlPortalAlias.Items.Add(item);
            }
        }
    }
}